import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mvvm_provider/page/bookshelf/view_model/book_shelf_view_model.dart';
import 'package:mvvm_provider/page/collection/view_model/collection_view_model.dart';
import 'package:mvvm_provider/page/home/view_model/home_view_model.dart';
import 'package:mvvm_provider/page/mine/view_model/mine_view_model.dart';
import 'package:mvvm_provider/page/novel/view_model/novel_view_model.dart';
import 'package:mvvm_provider/page/tabbar/view/tabber_page.dart';
import 'package:mvvm_provider/page/tabbar/view_model/tabber_view_model.dart';
import 'package:mvvm_provider/routers/routers.dart';
import 'package:mvvm_provider/until/shared_preferences.dart';
import 'package:provider/provider.dart';

import 'const/theme_provider.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  setPreferredOrientations();
  await SharedPreferencesUtil.init();
  runApp(
    /// 暗黑模式
    ChangeNotifierProvider(
      create: (_) => ThemeProvider(),
      child: MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
  @override
  void initState() {
    super.initState();
    Routes.initRoutes();
  }

  MaterialApp _materialApp(BuildContext context) {
    final themeProvider = context.watch<ThemeProvider>();
    return MaterialApp(
      home: MultiProvider(
        providers: [
          ChangeNotifierProvider<TabBarViewModel>(create: (_) => TabBarViewModel()),
          ChangeNotifierProvider<HomeViewModel>(create: (_) => HomeViewModel()),
          ChangeNotifierProvider<BookShelfViewModel>(create: (_) => BookShelfViewModel()),
          ChangeNotifierProvider<MineViewModel>(create: (_) => MineViewModel()),
          ChangeNotifierProvider<NovelViewModel>(create: (_) => NovelViewModel()),
        ],
        child: const TabbarPage(),
      ),
      builder: (context, widget) {
        return MediaQuery(
          ///设置文字大小不随系统设置改变
          data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
          child: FlutterEasyLoading(child: widget),
        );
      },
      navigatorKey: navigatorKey,
      locale: const Locale('zh', 'CH'),
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      supportedLocales: const [
        Locale('zh', 'CH'),
      ],
      theme: ThemeData.light(), // 浅色主题
      darkTheme: ThemeData.dark(), // 暗色主题
      themeMode: themeProvider.themeMode, // 动态主题模式
    );
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      builder: (BuildContext context, Widget? child) {
        return _materialApp(context);
      },
    );
  }
}

/// 强制app竖屏
void setPreferredOrientations() async {
  await SystemChrome.setPreferredOrientations(
    [
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ],
  );
}
